package cn.com.anypay.manager.common.enums;

import lombok.Getter;

/**
 * 统一结果状态码枚举
 */
@Getter
public enum ResultCode {

    // 成功
    SUCCESS(200, "操作成功"),

    // 客户端错误 4xx
    BAD_REQUEST(400, "请求参数错误"),
    UNAUTHORIZED(401, "未授权访问"),
    FORBIDDEN(403, "访问被拒绝"),
    NOT_FOUND(404, "资源不存在"),
    METHOD_NOT_ALLOWED(405, "请求方法不允许"),
    CONFLICT(409, "资源冲突"),
    UNPROCESSABLE_ENTITY(422, "请求参数验证失败"),
    TOO_MANY_REQUESTS(429, "请求过于频繁"),

    // 服务端错误 5xx
    SYSTEM_ERROR(500, "系统内部错误"),
    BAD_GATEWAY(502, "网关错误"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),
    GATEWAY_TIMEOUT(504, "网关超时"),

    // 业务错误 6xx
    BUSINESS_ERROR(600, "业务处理失败"),
    CUSTOM_FAIL(601, "自定义失败"),
    DATA_NOT_FOUND(602, "数据不存在"),
    DATA_ALREADY_EXISTS(603, "数据已存在"),
    OPERATION_NOT_ALLOWED(604, "操作不被允许"),

    // 数据库错误 7xx
    DATABASE_ERROR(700, "数据库操作失败"),
    DATABASE_CONNECTION_ERROR(701, "数据库连接失败"),
    SQL_SYNTAX_ERROR(702, "SQL语法错误"),

    // 第三方服务错误 8xx
    THIRD_PARTY_ERROR(800, "第三方服务错误"),
    PAYMENT_ERROR(801, "支付服务错误"),
    SMS_ERROR(802, "短信服务错误"),

    // 其他错误 9xx
    VALIDATION_ERROR(900, "参数校验失败"),
    NETWORK_ERROR(901, "网络连接错误"),
    TIMEOUT_ERROR(902, "请求超时"),
    UNKNOWN_ERROR(999, "未知错误");

    private final Integer code;
    private final String message;

    ResultCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 根据code获取枚举
     */
    public static ResultCode getByCode(Integer code) {
        for (ResultCode resultCode : values()) {
            if (resultCode.getCode().equals(code)) {
                return resultCode;
            }
        }
        return UNKNOWN_ERROR;
    }
}